#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

# Gets all colleges, all periods. Access ArtStor reports menu, ask for a csv download. Parse csv and update database.
#
# always returns all months and all colleges unless collegeID is passed as argument
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;

use avsCommon;


my ($content, $response, $request, $url);

dbsetup();

my $begperiod = myperiod($begyear, $begmonth);
my $endperiod = myperiod($endyear, $endmonth);

my ($viewstate, $eventvalidation);
my $ua = new LWP::UserAgent(keep_alive=>1);

# access vendor site to get logged in
getLogin();

# loop through all the colleges
do {
	getStats ();
} while ($sthlogin->fetch);	

dbshutdown();

exit;

sub getLogin {

	#set up to get the menu page
	$url='http://stats.artstor.org/ReportServer/Pages/ReportViewer.aspx?/Group/CCLA_DailyReport&rs:Command=Render';
	
	# create user agent, set login information and cookies
	$ua->credentials('stats.artstor.org:80', 'usnjpr1web02', $vendorusername, $vendorpassword);
	$ua->cookie_jar( {} );

	#get the menu page
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	$content=$response->decoded_content;

	$content =~ /VIEWSTATE\" value\=\"(?<viewstate>.*?)\".*?EVENTVALIDATION\" value\=\"(?<eventvalidation>.*?)\"/sm;
	$viewstate = urlencode($+{viewstate});
	$eventvalidation = urlencode($+{eventvalidation});
}

sub getStats {

	#set up to get the menu page
	$url='http://stats.artstor.org/ReportServer/Pages/ReportViewer.aspx?/Group/CCLA_DailyReport&rs:Command=Render';
	
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content('__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=' . $viewstate .
		'&ReportViewerControl%24ctl00%24ctl03%24ctl00=' . $option1 .
		'&ReportViewerControl%24ctl00%24ctl05%24ctl00=1&ReportViewerControl%24ctl00%24ctl00=View+Report&ReportViewerControl%24ctl01%24ctl05%24ctl00=Select+a+format&ReportViewerControl%24ctl04=&ReportViewerControl%24ctl05=&ReportViewerControl%24ctl06=1&ReportViewerControl%24ctl07=0&__EVENTVALIDATION=' . $eventvalidation ) ;

	$response = $ua->request($request);
	if (!$response->is_success) {
		die "#error:$0: POST report failed $url : " . $response->status_line;
	};
	$content = $response->decoded_content;

#	#extract execution and control id's
	$content =~ m/ExecutionID\=(?<executionid>.*?)\&ControlID\=(?<controlid>.*?)\&/;
	my $executionid=urlencode($+{executionid});
	my $controlid=urlencode($+{controlid});

  $url="http://stats.artstor.org/ReportServer/Reserved.ReportViewerWebControl.axd?ExecutionID=$executionid&ControlID=$controlid&Culture=1033&UICulture=9&ReportStack=1&OpType=ReportArea&Controller=ClientControllerReportViewerControl&PageNumber=1&ZoomMode=Percent&ZoomPct=100&ReloadDocMap=true&EnableFindNext=False&LinkTarget=_top";

	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET report $url : " . $response->status_line;
	};

	$content=$response->decoded_content;
	
	# save contents for possible debugging
	savecontent('ArtStor', $collegeid, $content);
	
	#finally have the actual data. extract only the table we need from the html 
	$content =~ /Usage by Time and Event Type.*?<TABLE(.*?<\/TR>){3}(?<data>.*?)<\/TABLE>/s;
	my $data = $+{data};
	$data =~ s/<.*?>/ /g; 	# remove html
	$data =~ s/,//g; 				# remove commas
	$data =~ s/&nbsp;/0 /g; # convert blanks to 0

	while ($data =~ /.*?(?<year>\d{4})-(?<month>\d{2})\s*\d+\s*\d+\s*(?<searches>\d+)/sgc) {
		my $period = myperiod($+{year}, $+{month});

		# only get data in requested time period
		if ($begperiod le $period && $period le $endperiod) {
			print "$vendorid $collegeid $period 'ArtStor' 0 $+{searches}\n";
			$sthstats->execute($vendorid, $collegeid, 'ArtStor', $period, $+{searches}, 2)
				or die "#error: sthstats execute failed, check end of log";
		}
	}
}